package com.lee.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lee.www.Mapper.PermissionMapper;
import com.lee.www.Mapper.RoleMapper;
import com.lee.www.entity.Permission;
import com.lee.www.entity.Role;
import com.lee.www.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @GetMapping("/list")
    public List<Role> list() {
        return roleMapper.selectList(null);
    }

    @PostMapping()
    public void insert(Role role) {
        roleMapper.insert(role);
    }

    @GetMapping("/vo/list")
    public CommonResult voList() {
        List<Role> roles = roleMapper.selectList(null);
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            RoleVo roleVo = new RoleVo();
            String permissionStrId = role.getPermissionId();
            String[] permissionIds = permissionStrId.split(",");
            List<PermissionVo> permissionVos = new ArrayList<>();
            for (String permissionStr : permissionIds) {
                if (!permissionStr.isEmpty()) {
                    Long permissionId = Long.parseLong(permissionStr);
                    Permission permission = permissionMapper.selectById(permissionId);
                    PermissionVo permissionVo = new PermissionVo();
                    // Permission拷贝
                    BeanUtils.copyProperties(permission, permissionVo);
                    permissionVos.add(permissionVo);
                }
            }
            // role拷贝
            BeanUtils.copyProperties(role, roleVo);
            roleVo.setPermissionVoList(permissionVos);
            roleVos.add(roleVo);
        }
        MsgInfo msgInfo = MsgInfo.initMsgInfo(200L, "数据获取成功");
        return CommonResult.initCommonResult(roleVos, msgInfo);
    }

    // 返回用户的两级权限
    @GetMapping("/vo/tree")
    public CommonResult voTree() {
        List<Role> roles = roleMapper.selectList(null);
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(role, roleVo);
            String permissionStrId = role.getPermissionId();
            String[] permissionIds = permissionStrId.split(",");
            List<PermissionVo> permission1Vos = new ArrayList<>();
            for (String permissionStr : permissionIds) {
                if (!permissionStr.isEmpty()) {
                    Long permissionId = Long.parseLong(permissionStr);
                    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
                    queryWrapper
                            .eq("pid", 0L)
                            .eq("id", permissionId);
                    List<Permission> permissions = permissionMapper.selectList(queryWrapper);
                    for (Permission permission : permissions) {
                        if (permission != null) {
                            PermissionVo permissionVo = new PermissionVo();
                            BeanUtils.copyProperties(permission, permissionVo);
                            QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
                            queryWrapper2
                                    .eq("pid", permission.getId());
                            List<Permission> permission2 = permissionMapper.selectList(queryWrapper2);
                            List<Permission2Vo> permission2Vos = new ArrayList<>();
                            for (Permission permissionTmp : permission2) {
                                Permission2Vo permission2Vo = new Permission2Vo();
                                BeanUtils.copyProperties(permissionTmp, permission2Vo);
                                permission2Vos.add(permission2Vo);
                            }
                            permissionVo.setChildren(permission2Vos);
                            permission1Vos.add(permissionVo);
                        }
                    }
                }
            }
            roleVo.setPermissionVoList(permission1Vos);
            roleVos.add(roleVo);
        }
        MsgInfo msgInfo = MsgInfo.initMsgInfo(200L, "获取成功");
        return CommonResult.initCommonResult(roleVos, msgInfo);
    }
}